package pers.hxy.lookweather.view.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.qweather.sdk.bean.weather.WeatherDailyBean;

import java.util.List;

import pers.hxy.lookweather.R;
import pers.hxy.lookweather.utils.IconUtil;

public class Weather7DRecyclerViewAdapter extends RecyclerView.Adapter<Weather7DRecyclerViewAdapter.ViewHolder> {
    private List<WeatherDailyBean.DailyBean> dailyBeans;

    public Weather7DRecyclerViewAdapter(List<WeatherDailyBean.DailyBean> dailyBeans) {
        this.dailyBeans = dailyBeans;
    }

    public void setDailyBeans(List<WeatherDailyBean.DailyBean> dailyBeans) {
        this.dailyBeans = dailyBeans;
    }

    /**
     * @param parent   The ViewGroup into which the new View will be added after it is bound to
     *                 an adapter position.
     * @param viewType The view type of the new View.
     * @return
     */
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_weather_7d, parent, false);
        return new ViewHolder(view);
    }

    /**
     * @param holder   The ViewHolder which should be updated to represent the contents of the
     *                 item at the given position in the data set.
     * @param position The position of the item within the adapter's data set.
     */
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        WeatherDailyBean.DailyBean bean = dailyBeans.get(position);
        holder.tvDate.setText(bean.getFxDate().substring(5));
        holder.tvTemp.setText(bean.getTempMin() + "℃/" + bean.getTempMax() + "℃");
        holder.tvText.setText("白天:" + bean.getTextDay() + " / 晚上:" + bean.getTextNight());
        holder.ivIconDay.setImageResource(IconUtil.getIconID(bean.getIconDay()));
        holder.ivIconNight.setImageResource(IconUtil.getIconID(bean.getIconNight()));
        holder.tvPrecip.setText("降水量" + bean.getPrecip() + "mm");
    }

    /**
     * @return
     */
    @Override
    public int getItemCount() {
        return dailyBeans.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvDate;
        TextView tvTemp;
        TextView tvText;
        ImageView ivIconDay;
        ImageView ivIconNight;
        TextView tvPrecip;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDate = itemView.findViewById(R.id.tv_item_7d_date);
            tvTemp = itemView.findViewById(R.id.tv_item_7d_temp);
            tvText = itemView.findViewById(R.id.tv_item_7d_text);
            ivIconDay = itemView.findViewById(R.id.iv_item_7d_icon_day);
            ivIconNight = itemView.findViewById(R.id.iv_item_7d_icon_night);
            tvPrecip = itemView.findViewById(R.id.tv_item_7d_precip);
        }
    }
}
